package com.eulog.gwt.axisdesign.propertyA.client;

import java.util.ArrayList;

import com.eulog.gwt.euloggwtlibrary.user.client.ui.EulogImage2;
import com.eulog.gwt.euloggwtlibrary.user.client.ui.MouseEventImage;
import com.eulog.gwt.euloggwtlibrary.user.client.ui.MouseEventWidgetCollection;
import com.extjs.gxt.ui.client.Style.HorizontalAlignment;
import com.extjs.gxt.ui.client.fx.FxConfig;
import com.extjs.gxt.ui.client.widget.LayoutContainer;
import com.extjs.gxt.ui.client.widget.layout.TableLayout;
import com.google.gwt.user.client.Element;
import com.google.gwt.user.client.ui.ClickListener;
import com.google.gwt.user.client.ui.Widget;

/*
 * image index number of filename start from 1, ex) image1.png image2.png
 * image name has not zero padding, ex image1.png~image1xx.png, image1.png~image1x.png
 */
public class SlideShow {

	final protected int imageCount;
	protected int thumbsPerPage;
	protected int totalThumbsPage;

	
	protected MouseEventWidgetCollection<MouseEventImage> images;
	public int targetThumbSlidePage;
	
	public LayoutContainer panel;
	public LayoutContainer imagePanel;

	ArrayList<LayoutContainer> thumbPanels;
	public LayoutContainer thumbPanel;
	public LayoutContainer thumbSlidePanel;
	public LayoutContainer thumbControlPanel;
	
	String imageNamePreFix;
	String imageNamePostFix;
	String thumbImageNamePreFix;
	String thumbImageNamePostFix;
	
	public SlideShow(
			int _imageCount,
			int _thumbsPerPage,
			String _imageNamePreFix,
			String _imageNamePostFix,
			String _thumbImageNamePreFix,
			String _thumbImageNamePostFix) {
		
		imageCount = _imageCount;
		thumbsPerPage = _thumbsPerPage;
		
		imageNamePreFix = _imageNamePreFix;
		imageNamePostFix = _imageNamePostFix;
		thumbImageNamePreFix = _thumbImageNamePreFix;
		thumbImageNamePostFix = _thumbImageNamePostFix;
		
		init();
	}
	
	private void init() {
		images = new MouseEventWidgetCollection<MouseEventImage>();
		thumbPanels = new ArrayList<LayoutContainer>();
		
		createControllerImages();
		createThumbImages();
		
		
		targetThumbSlidePage = 0;
		totalThumbsPage =  (int) Math.ceil( (double)imageCount/thumbsPerPage);
		targetThumbSlidePage = 0;
		
		initSample299a();
	}
	
	public LayoutContainer getImagePanel() {return imagePanel;}
	
	protected void initSample299a() {
		panel = createMasterPanel();
		imagePanel = createImagePanel();
		
		LayoutContainer separator = new LayoutContainer();
		separator.setHeight(6);

		LayoutContainer bottomPanel = new LayoutContainer() {
			@Override
			protected void onRender(Element parent, int pos) {
				super.onRender(parent, pos);
				//setStyleAttribute("background", "black");
				TableLayout layout = new TableLayout(1);
			    layout.setCellHorizontalAlign(HorizontalAlignment.CENTER);
			    layout.setWidth("100%");
			    setLayout(layout);
			    layout();
			}
		};
		
		thumbControlPanel=createThumbSlideControllerPanel();
		thumbSlidePanel = new LayoutContainer();
		thumbPanel=createThumbPanel();
		thumbSlidePanel.add(thumbPanel);
		
		panel.add(imagePanel);
		panel.add(separator);

		bottomPanel.add(thumbControlPanel);
		bottomPanel.add(thumbSlidePanel);
		bottomPanel.setStyleName("ThumbBottomPanel");
		panel.add(bottomPanel);
		
		panel.layout();
	}
	
	private void createControllerImages() {
		
		MasterRack.imageManager.controlLeft.addClickListener(new ClickListener () {
			public void onClick(Widget sender) {
				if(targetThumbSlidePage>0) {
					thumbSlideMove(targetThumbSlidePage,targetThumbSlidePage-1);
					targetThumbSlidePage--;
				}
			}
		});
		MasterRack.imageManager.controlRight.addClickListener(new ClickListener () {
			public void onClick(Widget sender) {
				if(targetThumbSlidePage<totalThumbsPage-1) {
					thumbSlideMove(targetThumbSlidePage,targetThumbSlidePage+1);
					targetThumbSlidePage++;
				}
			}
		});
		
	}
	
	LayoutContainer test1,test2;
	void thumbSlideMove(int before,int after) {
		thumbPanel.removeAll();
		thumbPanel.el().fadeIn(FxConfig.NONE);
		thumbPanel.add(thumbPanels.get(after));
		thumbPanel.layout();
	}
	
	public String getZeroPadding() {
		int totalImages = imageCount;
		String zeroPadding = "";
		while(totalImages>9) {
			zeroPadding+="0";
			totalImages/=10;
		}
		
		return zeroPadding;
	}
	
	public String getIndexWithZeroPadding(int index) {
		String zeroPadding = getZeroPadding();
		int tempIndex=index;
		while(tempIndex>9) {
			zeroPadding=zeroPadding.substring(1);
			tempIndex/=10;
		}
		return zeroPadding+index;
	}
	
	private void createThumbImages() {
		
		MouseEventImage tempImage;
		for(int i=1; i<=imageCount; i++) {
			tempImage = new MouseEventImage(thumbImageNamePreFix + i + thumbImageNamePostFix,"thumbImage");
			tempImage.addClickListener(images);
			images.add(tempImage);
			tempImage.addClickListener(new ClickListener() {
				public void onClick(Widget sender) {
					onClickThumbImage(sender);
				}
			});
		}
	}
	
	public void onClickThumbImage(Widget sender) {
		int index = images.indexOf(sender) + 1;
		imagePanel.el().fadeIn(FxConfig.NONE);
		imageChange(index);
	}
	
	public void imageChange(int index) {
		imagePanel.removeAll();
		imagePanel.add(new EulogImage2(imageNamePreFix + index + imageNamePostFix,false,false,true));
		imagePanel.layout();	
	}
	
	private LayoutContainer createMasterPanel() {
		LayoutContainer newPanel = new LayoutContainer();
		return newPanel;
	}
	
	private LayoutContainer createImagePanel() {
		LayoutContainer newImagePanel = new LayoutContainer() {
			@Override
			protected void onRender(Element parent, int index) {
				super.onRender(parent, index);
				setStyleAttribute("background", "black");
			}
		};
		return newImagePanel;
	}
	
	private LayoutContainer createThumbSlideControllerPanel() {
		LayoutContainer newThumbControlContainer = new LayoutContainer();
		newThumbControlContainer.add(MasterRack.imageManager.controlLeft);
		newThumbControlContainer.add(MasterRack.imageManager.controlRight);
		return newThumbControlContainer;
	}
	
	private LayoutContainer createThumbPanel() {
		
		LayoutContainer newThumbPanel= new LayoutContainer() {
			@Override
			protected void onRender(Element parent, int index) {
				super.onRender(parent, index);
				setStyleAttribute("overflow", "visible");
			}
		};
		
		LayoutContainer tempcontainer = new LayoutContainer();
		for(int i=0; i<imageCount; i++) {
			if(i%thumbsPerPage == 0) {
				tempcontainer = new LayoutContainer();
				thumbPanels.add(tempcontainer);
			}
			tempcontainer.add(images.get(i));
		}
		newThumbPanel.add(thumbPanels.get(0));
		
		return newThumbPanel;
	}
}
